package google.entry;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhongwei on 2016/12/25.
 */
public class ImageWorker extends Thread {
    static Pattern pattern = Pattern.compile("id=\"resultStats\">(.+?)<nobr>", Pattern.DOTALL);
    File task;
    File output;
    FileWriter fileWriter;
    private String urlModel = "https://www.ytso.cc/search?q=[KEYWORD]&hl=zh-CN&tbs=cdr:1,cd_min:[START],cd_max:[END]&source=lnms&tbm=nws";

    public ImageWorker(File task, File output) {
        this.task = task;
        this.output = output;
        try {
            fileWriter = new FileWriter(output, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String processOneUrl(String url, String start, String end) {
        try {
            Document document = Jsoup.connect(url).validateTLSCertificates(false).header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0").get();
            ;
            String content = document.html();
            Matcher matcher = pattern.matcher(content);
            if (matcher.find()) {
                String string = matcher.group(1);
                string = string.replaceAll("[\\u4e00-\\u9fa5]", "").replaceAll(",", "").trim();
                System.out.println(getName() + " : " + string);
                return string;
            }
            if (content.contains("请检查输入字词有无错误"))
                return "0";
        } catch (Exception ex) {
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return processOneUrl(url, start, end);
        }
        return "";
    }

    public void work() throws Exception {
        List<String> tasks = FileUtils.readLines(task);
        Set<String> set = new HashSet<>();
        List<String> lines = FileUtils.readLines(output, "UTF-8");
        for (String str : lines) {
            String[] arr = str.split("\t");
            set.add(arr[0] + "\t" + arr[1]);
        }
        for (String str : tasks) {
            if (!set.contains(str + "\t2014/1/4" + "-2014/12/31")) {
                String number = processOneUrl(urlModel.replace("[KEYWORD]", URLEncoder.encode(str)).replace("[START]", "2014/1/4").replace("[END]", "2014/12/31"), "2014/1/4", "2014/12/31");
                fileWriter.append(str + "\t2014/1/4" + "-2014/12/31\t" + number + "\n");
            }
            for (int i = 2005; i <= 2015; i++) {
                String start = i + "/1/1";
                String end = i + "/12/31";
                if (set.contains(str + "\t" + start + "-" + end)) continue;
                String num = processOneUrl(urlModel.replace("[KEYWORD]", URLEncoder.encode(str)).replace("[START]", start).replace("[END]", end), start, end);
                fileWriter.append(str + "\t" + start + "-" + end + "\t" + num + "\n");
            }
            if (!set.contains(str + "\t2016/1/1-2016/4/24")) {
                String num = processOneUrl(urlModel.replace("[KEYWORD]", URLEncoder.encode(str)).replace("[START]", "2016/1/1").replace("[END]", "2016/4/24"), "2016/1/1", "2016/4/24");
                fileWriter.append(str + "\t2014/1/4-2014/12/31\t" + num + "\n");
            }
            fileWriter.flush();
        }
    }

    @Override
    public void run() {
        try {
            work();
        } catch (Exception e) {
            try {
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }
}
